﻿using BZ.Core;
using NDK.Motion.Card;
using System;
using System.Diagnostics;
using System.Threading;

namespace NDK.Motion.MachineResources
{
    [Serializable]
    public class InputSetting
    {
        #region Model
        private int _Id;
        private string _InputName;
        private int _ModuleName;
        private int _Port;
        private bool _Status;
        private ICard _card;
        private EFFECTIVE_LEVEL_ENUM _EffectiveLevel =  EFFECTIVE_LEVEL_ENUM.High;//有效电平：true:高电平有效；false:低电平有效
        /// <summary>
        /// 
        /// </summary>
        public EFFECTIVE_LEVEL_ENUM EffectiveLevel
        {
            set { _EffectiveLevel = value; }
            get { return _EffectiveLevel; }
        }
        public InputSetting()
        {
        }

        public InputSetting(string name, ICard card, int port, int group = 0)
        {
            this._card = card;
            this._Port = port;
            this._InputName = name;
            this.Group = group;
        }
       
        public int ID
        {
            set { _Id = value; }
            get { return _Id; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Name
        {
            set { _InputName = value; }
            get { return _InputName; }
        }
        /// <summary>
        /// 端口所在模块，凌华总线卡一般就是从站号，脉冲卡就是卡号
        /// </summary>
        public int ModuleName
        {
            set { _ModuleName = value; }
            get { return _ModuleName; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int Port
        {
            set { _Port = value; }
            get { return _Port; }
        }

        public ICard Card
        {
            get { return _card; }
            set { _card = value; }
        }

        //用于界面绑定显示
        public string CardType { get { return Card?.CardType; } }
        public int? CardID { get { return Card?.CardID; } }

        public int Group { get; set; } = 0;
        #endregion Model

        /// <summary>
        /// 获取Input状态
        /// </summary>
        /// <returns>高电平有效：高电平返回true，低电平返回false</returns>
        /// <returns>低电平有效：高电平返回false，低电平返回true</returns>
        /// <Exception>异常抛出，读取失败抛出异常</Exception>
        public bool GetStatus()
        {
            bool ret = false;
            try
            {
                ret = Card.GetDI(_ModuleName, _Port);
            }
            catch (Exception ex)
            {
                throw new IOException("获取Input[:" + _InputName + "]状态时发生异常！" + ex.Message);
            }
            if (_EffectiveLevel ==  EFFECTIVE_LEVEL_ENUM.High)
            {
                return ret;
            }
            else
            {
                return !ret;
            }
        }
        /// <summary>
        /// 等待Input状态为有效电平
        /// </summary>
        /// <param name="timeout">超时时间</param>
        /// <returns>返回true：超时</returns>
        public bool WaitON(int timeout = int.MaxValue)
        {
            bool isTimeout = false;
            Stopwatch sw = new Stopwatch();
            sw.Restart();
            try
            {
                while (GetStatus() == false)
                {
                    if (sw.ElapsedMilliseconds >= timeout)
                    {
                        return true;
                    }
                    Thread.Sleep(1);
                }
            }
            catch (Exception ex)
            {
                throw new IOException("InputSetting WaitON Exception:" + ex.Message);
            }
            return isTimeout;
        }
        /// <summary>
        /// 等待Input状态为无效电平
        /// </summary>
        /// <param name="timeout">超时时间</param>
        /// <returns>返回true：超时</returns>
        public bool WaitOFF(int timeout = int.MaxValue)
        {
            bool isTimeout = false;
            Stopwatch sw = new Stopwatch();
            sw.Restart();
            try
            {
                while (GetStatus() == true)
                {
                    if (sw.ElapsedMilliseconds >= timeout)
                    {
                        return true;
                    }
                    Thread.Sleep(1);
                }
            }
            catch (Exception ex)
            {
                throw new IOException("InputSetting WaitOFF Exception:" + ex.Message);
            }
            return isTimeout;
        }

        /// <summary>
        /// 保持一定时间的有效电平，默认10秒
        /// </summary>
        /// <param name="keepTime">保持时间</param>
        /// <returns></returns>
        public bool KeepON(int keepTime = 10000)
        {
            Stopwatch sw = new Stopwatch();
            sw.Restart();
            try
            {
                while (GetStatus() == true)
                {
                    if (sw.ElapsedMilliseconds >= keepTime)
                    {
                        return true;
                    }
                    Thread.Sleep(10);
                }
            }
            catch (Exception ex)
            {
                throw new IOException("InputSetting KeepON Exception:" + ex.Message);
            }
            return false;
        }
        /// <summary>
        /// 保持一定时间的无效电平，默认10秒
        /// </summary>
        /// <param name="keepTime">保持时间</param>
        /// <returns></returns>
        public bool KeepOFF(int keepTime = 10000)
        {
            Stopwatch sw = new Stopwatch();
            sw.Restart();
            try
            {
                while (GetStatus() == false)
                {
                    if (sw.ElapsedMilliseconds >= keepTime)
                    {
                        return true;
                    }
                    Thread.Sleep(10);
                }
            }
            catch (Exception ex)
            {
                throw new IOException("InputSetting KeepOFF Exception:" + ex.Message);
            }
            return false;
        }

    }
}
